package com.itheima.listener;

import com.itheima.cst.SysConsts;
import com.itheima.service.MyUserService;

import com.itheima.utils.RedisUtil;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.lang.Runtime.*;
//BaseApplicationListener
@Component
public class MyStartupListener implements BaseApplicationListener{

    @Resource
    private MyUserService myUserService;

    private ExecutorService executorService = Executors.newFixedThreadPool(getRuntime().availableProcessors() * 2);

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        executorService.execute(() -> {
            myUserService.queryUser().stream().forEach(user -> {
                RedisUtil.set(SysConsts.REDIS_USER_PREFIX + user.getUserName(),user);
            });
        });
    }

}